'use strict';
const BaseService = require('./base');

class PermissionService extends BaseService {


    isAdmin(roles){
        let isAdminFlag = false;
        if(roles && Array.isArray(roles) && roles.length >0){
            for(let i=0;i<roles.length;i++){
                if(roles[i].roleId == 1){
                    isAdminFlag = true;
                    break;
                }
            }
            return isAdminFlag;
        }
        return isAdminFlag;
    }

    async getRolePermission(user){
        let roles = [];
        if(this.isAdmin(user.roles)){
            roles.push('admin');
        }else{
            let list = await this.ctx.service.sysrole.selectRolePermissionByUserId(user.userId);
            roles = roles.concat(list);
        }
        return roles;
    }


    async getMenuPermission(user){
        let perms = [];
        if(this.isAdmin(user.roles)){
            perms.push("*:*:*");
        }else{
            let list = await this.ctx.service.sysmenu.selectMenuPermsByUserId(user.userId);
            perms = perms.concat(list);
        }
        return perms;
    }

}

module.exports = PermissionService;
